
$(document).ready(function() {
	
	
    validateForm($("#categoryAdd"));
    addCustomRule();
    initRules();
});

/**
 *validate 
 */
function validateForm($form){
	$form.validate({
		ignore: "",
        onfocusout : function( element, event ) {
		    if ($(element).valid()){
        		$(element).closest('div.admin_control_row').find('.category_group_warntip').removeClass("show");
		    	$(element).closest('div.admin_control_row').find('.category_group_warntip').addClass("hide");
		    }
		},
        onkeyup: function( element, event ) {
		    $(element).valid();
		},
		
        errorPlacement: function (error, element) {
            $(element).closest('div.admin_control_row').find('.category_group_warntip').removeClass("hide");
            $(element).closest('div.admin_control_row').find('.category_group_warntip').addClass("show");
        },
        invalidHandler: function(form, validator) {
            if (!validator.numberOfInvalids()){
                return;
            }
            scrollToTopElement($(validator.errorList[0].element));
        }
    });
    
}
function initRules(){
	$('#category_name').rules("add", {
            required : true,
            minlength: 2,
     });
}
function scrollToTopElement($element){
	if($element.hasClass('postImage')){
		$element = $('.element-images');
	}
	//posting_element
    $('html, body').animate({
        scrollTop: $element.offset().top
    }, 100);
}
/**
 * add rule
 */
 function addCustomRule(){
 	 $.validator.addMethod('validateSelect', function (value, element, arg) {
        return arg != value;
    }, 'Vui lòng chọn');
 }

